home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 June: Reference Library / Dev.CD Jun 96 RL / Dev.CD Jun 96 RL.toast / Technical Documentation / develop / develop Issue 24 / develop Issue 24 code / Scriptable Database 1.0a15 / Blue / FileIterators.cp < prev    next >
Encoding:
Text File  |  1996-02-19  |  7.5 KB  |  213 lines  |  [TEXT/CWIE]

  1.  
  2. #include "FileIterators.h"
  3. #include "FSSpecification.h"
  4.  
  5. //========================================================================================
  6. //    CLASS TAbstractFileIterator
  7. //========================================================================================
  8.  
  9. //----------------------------------------------------------------------------------------
  10. // TAbstractFileIterator::~TAbstractFileIterator
  11. //----------------------------------------------------------------------------------------
  12. TAbstractFileIterator::~TAbstractFileIterator()
  13. {
  14. } // TAbstractFileIterator::~TAbstractFileIterator 
  15.  
  16.  
  17. //========================================================================================
  18. //    CLASS TCatalogSearchIterator
  19. //========================================================================================
  20.  
  21.  
  22. //----------------------------------------------------------------------------------------
  23. // TCatalogSearchIterator::TCatalogSearchIterator
  24. //----------------------------------------------------------------------------------------
  25. TCatalogSearchIterator::TCatalogSearchIterator(SInt32 vRefNum, CInfoPBRec& searchSpec, CInfoPBRec& searchMask, SInt32 flags)
  26. {
  27.     fCatSearchBuffer = nil;
  28.     fResultsBuffer = nil;
  29.     fCurrentEntry = 0;
  30.     fEntriesInBuffer = 0;
  31.     fCatsearchHasMoreDataToReturn = false;
  32.  
  33.     this->InitializeParamBlock(vRefNum, searchSpec, searchMask, flags);
  34. } // TCatalogSearchIterator::TCatalogSearchIterator 
  35.  
  36. //----------------------------------------------------------------------------------------
  37. // TCatalogSearchIterator::TCatalogSearchIterator: 
  38. //----------------------------------------------------------------------------------------
  39. TCatalogSearchIterator::TCatalogSearchIterator(SInt32 vRefNum, unsigned char* fileName, OSType fileType /* = 0*/, OSType fileCreator /*= 0*/, SInt32 flags /*= 0*/)
  40. {
  41.     fCatSearchBuffer = nil;
  42.     fResultsBuffer = nil;
  43.     fCurrentEntry = 0;
  44.     fEntriesInBuffer = 0;
  45.     fCatsearchHasMoreDataToReturn = false;
  46.  
  47.     CInfoPBRec searchSpec;
  48.     CInfoPBRec searchMask;
  49.  
  50.     //
  51.     // Zero out important fields in the search mask
  52.     //
  53.     searchMask.hFileInfo.ioFlAttrib = 0;
  54.     searchMask.hFileInfo.ioFlFndrInfo.fdLocation.v = 0;
  55.     searchMask.hFileInfo.ioFlFndrInfo.fdLocation.h = 0;
  56.     searchMask.hFileInfo.ioFlFndrInfo.fdFldr = 0;
  57.     searchMask.hFileInfo.ioFlFndrInfo.fdFlags = 0;        
  58.     searchMask.hFileInfo.ioFlFndrInfo.fdType = 0;
  59.     searchMask.hFileInfo.ioFlFndrInfo.fdCreator = 0;
  60.     
  61.     if(fileType != 0)
  62.     {
  63.         searchSpec.hFileInfo.ioFlFndrInfo.fdType = fileType;
  64.         searchMask.hFileInfo.ioFlFndrInfo.fdType = 0xFFFFFFFF;
  65.     }
  66.     
  67.     if(fileCreator != 0)
  68.     {
  69.         searchSpec.hFileInfo.ioFlFndrInfo.fdCreator = fileCreator;
  70.         searchMask.hFileInfo.ioFlFndrInfo.fdCreator = 0xFFFFFFFF;
  71.     }
  72.     
  73.     searchSpec.hFileInfo.ioNamePtr = (StringPtr)fileName;
  74.  
  75.     this->InitializeParamBlock(vRefNum, searchSpec, searchMask, flags);    
  76. } // TCatalogSearchIterator::TCatalogSearchIterator 
  77.  
  78. //----------------------------------------------------------------------------------------
  79. // TCatalogSearchIterator::~TCatalogSearchIterator: 
  80. //----------------------------------------------------------------------------------------
  81. TCatalogSearchIterator::~TCatalogSearchIterator()
  82. {
  83.     if(fCatSearchBuffer != nil)
  84.         DisposeHandle(fCatSearchBuffer);
  85.     if(fResultsBuffer != nil)
  86.         DisposeHandle((Handle)fResultsBuffer);
  87.  
  88. } // TCatalogSearchIterator::~TCatalogSearchIterator 
  89.  
  90. //----------------------------------------------------------------------------------------
  91. // TCatalogSearchIterator::More: 
  92. //----------------------------------------------------------------------------------------
  93. Boolean TCatalogSearchIterator::More() const
  94. {
  95.     return ((fEntriesInBuffer > fCurrentEntry) || fCatsearchHasMoreDataToReturn);
  96. } // TCatalogSearchIterator::More 
  97.  
  98. //----------------------------------------------------------------------------------------
  99. // TCatalogSearchIterator::Next: 
  100. //----------------------------------------------------------------------------------------
  101. void TCatalogSearchIterator::Next()
  102. {
  103.     if(this->More())
  104.     {
  105.         ++fCurrentEntry;
  106.         if(fCurrentEntry >= fEntriesInBuffer)
  107.             this->ReadMoreInfoViaCatSearch();
  108.     }
  109. } // TCatalogSearchIterator::Next 
  110.  
  111. //----------------------------------------------------------------------------------------
  112. // TCatalogSearchIterator::Current: 
  113. //----------------------------------------------------------------------------------------
  114. TFSSpecification TCatalogSearchIterator::Current() const
  115. {
  116.     return TFSSpecification((*fResultsBuffer)[fCurrentEntry]);
  117. } // TCatalogSearchIterator::Current 
  118.  
  119. //----------------------------------------------------------------------------------------
  120. // TCatalogSearchIterator::InitializeParamBlock: 
  121. //----------------------------------------------------------------------------------------
  122. void TCatalogSearchIterator::InitializeParamBlock(SInt32 vRefNum, CInfoPBRec& searchSpec, CInfoPBRec& searchMask, SInt32 flags)
  123. {
  124.     //
  125.     // Set up search spec and search mask
  126.     //
  127.     fSearchSpec = searchSpec;
  128.     fSearchMask = searchMask;
  129.     
  130.     //
  131.     // Make sure that the directory bits are set if our flags
  132.     // indicate that we should search only files or only directories
  133.     //
  134.     if((flags & (kSeachFilesAndDirectories)) == kSeachFilesAndDirectories)
  135.     {
  136.         fSearchMask.hFileInfo.ioFlAttrib &= ~ioDirMask;
  137.     }
  138.     else if((flags & (kSeachFilesOnly | kSearchDirectoriesOnly)) != 0)
  139.     {
  140.         if(flags & kSeachFilesOnly)
  141.             fSearchSpec.hFileInfo.ioFlAttrib &= ~ioDirMask;
  142.         else
  143.             fSearchSpec.hFileInfo.ioFlAttrib |= ioDirMask;
  144.         fSearchMask.hFileInfo.ioFlAttrib |= ioDirMask;
  145.     }
  146.     
  147.     //
  148.     // Set up paramater block to point to search spec and search mask
  149.     //
  150.     fParamBlock.ioSearchInfo1 = &fSearchSpec;
  151.     fParamBlock.ioSearchInfo2 = &fSearchMask;
  152.     fParamBlock.ioNamePtr = nil;
  153.     fParamBlock.ioVRefNum = vRefNum;
  154.  
  155.     fParamBlock.ioCatPosition.initialize = 0;        // Start at the beginning
  156.     fParamBlock.ioSearchTime = 300;                    // Time out every 1/2 sec
  157.     fParamBlock.ioActMatchCount = 0;
  158.     
  159.     //
  160.     // Always search based on finder info and folder attributes;
  161.     // also search based on name if ioNamePtr is not nil.
  162.     //
  163.     fParamBlock.ioSearchBits = fsSBFlFndrInfo | fsSBFlAttrib;
  164.     if(fSearchSpec.hFileInfo.ioNamePtr != nil)
  165.         fParamBlock.ioSearchBits |= (((flags & kNameMustMatchExactly) != 0) ? fsSBFullName : fsSBPartialName);
  166.     
  167.     //
  168.     // Set up buffers
  169.     //
  170.     fParamBlock.ioOptBufSize = 0x4000L;
  171.     fParamBlock.ioReqMatchCount = (flags & kOnlyOneMatch) != 0 ? 1 : 10;
  172.     
  173.     fCatSearchBuffer = NewHandle(fParamBlock.ioOptBufSize);
  174.     fResultsBuffer = (FSSpec**)NewHandle(sizeof(FSSpec) * (fParamBlock.ioReqMatchCount + 1));
  175.  
  176.     if((fCatSearchBuffer != nil) && (fResultsBuffer != nil))
  177.     {
  178.         HLock(fCatSearchBuffer);
  179.         fParamBlock.ioOptBuffer = *fCatSearchBuffer;
  180.         HLock((Handle)fResultsBuffer);
  181.         fParamBlock.ioMatchPtr = *fResultsBuffer;
  182.         
  183.         fCatsearchHasMoreDataToReturn = true;
  184.         this->ReadMoreInfoViaCatSearch();
  185.     }
  186. } // TCatalogSearchIterator::InitializeParamBlock 
  187.  
  188. //----------------------------------------------------------------------------------------
  189. // TCatalogSearchIterator::ReadMoreInfoViaCatSearch: 
  190. //----------------------------------------------------------------------------------------
  191. void TCatalogSearchIterator::ReadMoreInfoViaCatSearch()
  192. {
  193.     fCurrentEntry = 0;
  194.     fEntriesInBuffer = 0;
  195.     
  196.     while((fEntriesInBuffer == 0) && (fCatsearchHasMoreDataToReturn == true))
  197.     {
  198.         OSErr err = PBCatSearchSync(&fParamBlock);
  199.         fEntriesInBuffer = fParamBlock.ioActMatchCount & 0x0000FFFF;
  200.         fCatsearchHasMoreDataToReturn = (err == noErr);
  201.     }
  202.     
  203.     //
  204.     // fParamBlock.ioReqMatchCount == 1 iff we are interested
  205.     // in only the first match from catsearch
  206.     //
  207.     if(fParamBlock.ioReqMatchCount > 1)
  208.         fCatsearchHasMoreDataToReturn = false;
  209. } // TCatalogSearchIterator::ReadMoreInfoViaCatSearch 
  210.  
  211.  
  212.  
  213.